package cn.itsource.basic.controller;

import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.utils.FastDfsApiOpr;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastdfs")
public class FastDfsController {

    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file){
        try {
            //获取文件附件名   1.jpg
            String filename = file.getOriginalFilename();
            //获取后缀名    D:/a/b/c/d/e.jpg
            String extension = FilenameUtils.getExtension(filename);

            //上传附件(返回附件对应的url地址)
            String upload = FastDfsApiOpr.upload(file.getBytes(), extension);
            return AjaxResult.me().setResultObj(upload);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }

    }
}
